package com.study.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * 两个部分组成切面
 *      1. 额外功能
 *          public class MyAround implements MethodInterceptor {
 *
 *              @Override
 *              public Object invoke(MethodInvocation invocation) throws Throwable {
 *                  System.out.println("额外功能");
 *                  Object o = invocation.proceed();
 *                  return o;
 *              }
 *          }
 *      2. 切入点
 *          <aop:config>
 *              <aop:pointcut id="" expresstion="execution(* login(..))"></aop:pointcut>
 *          </aop:config>
 * @Author chenyeqing
 * @Date 2021/10/13 22:46
 */
@Aspect
public class MyAspect {

    /**
     * 切入点的复用
     */
    @Pointcut("execution(* *..UserServiceImpl.*(..))")
    public void pointcut() {}

    @Around(value = "pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) {
        Object proceed = null;
        System.out.println("----aspect log----");
        try {
            proceed = joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return proceed;
    }

    @Around(value = "pointcut()")
    public Object around1(ProceedingJoinPoint joinPoint) {
        Object proceed = null;
        System.out.println("----aspect tx----");
        try {
            proceed = joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return proceed;
    }
}
